﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace PS.Framework.EventLogger
{
    public static class PSEV
    {
        private static LoggerConfiguration _config;
        private static ILogEngine _logEngine;
        private static bool _bInitialized = false;
        private static object _oLock = new object();

        /// <summary>
        /// Method to initialize the event logger
        /// </summary>
        public static void Initialize()
        {
            PSEV._config = LoggerConfiguration.LoadConfiguration();
            switch (PSEV._config.LogType)
            {
                case LogType.XmlLog:
                    {
                        PSEV._logEngine = new XmlLogger();
                        PSEV._logEngine.Configuration = PSEV._config;
                        break;
                    }
                case LogType.EventLog:
                    {
                        PSEV._logEngine = new EventLogger();
                        PSEV._logEngine.Configuration = PSEV._config;
                        break;
                    }
            }
            PSEV._bInitialized = true;
        }

        /// <summary>
        /// Method to uninitialize the event logger
        /// </summary>
        public static void Uninitialize()
        {
            PSEV._logEngine.Dispose();
            PSEV._bInitialized = false;
        }

        /// <summary>
        /// Method to write an event
        /// </summary>
        /// <param name="message">the message to log</param>
        /// <param name="exception">if an exception occured the whole exception</param>
        /// <param name="type">the type of the log entry</param>
        public static void WriteEvent(string message, Exception exception, LogEntryType type)
        {
            if (PSEV._bInitialized)
            {
                object oLock;
                Monitor.Enter(oLock = PSEV._oLock);
                try
                {
                    PSEV._logEngine.WriteLog(message, exception, type);
                }
                finally
                {
                    Monitor.Exit(oLock);
                }
            }
        }
        //public static void ShowLogEntries()
        //{
        //    if (PSEV._bInitialized)
        //    {
        //        object oLock;
        //        Monitor.Enter(oLock = PSEV._oLock);
        //        try
        //        {
        //            List<LogEntry> list = new List<LogEntry>();
        //            list.AddRange(PSEV._logEngine.GetLogEntries());
        //            LogViewer logViewer = new LogViewer(list);
        //            logViewer.Show();
        //            return;
        //        }
        //        finally
        //        {
        //            Monitor.Exit(oLock);
        //        }
        //    }
        //    throw new InvalidOperationException("The LogEngine is not initialized.");
        //}

        /// <summary>
        /// Method to get all log entries
        /// </summary>
        /// <returns>all log entries</returns>
        public static LogEntry[] GetLogEntries()
        {
            return PSEV._logEngine.GetLogEntries();
        }

        /// <summary>
        /// Method to create the log if the application log mode is EventLog.
        /// </summary>
        public static void CreateEventLog()
        {
            if (!PSEV._bInitialized)
            {
                PSEV.Initialize();
            }
            if (PSEV._config.LogType == LogType.EventLog)
            {
                PSEV.WriteEvent("Eventlog wurde erstellt.", null, LogEntryType.SuccessAudit);
            }
            PSEV.Uninitialize();
        }
    }
}
